function overLimit() {
	log("overLimit: ", 0);
	jQuery('#surplus').html(0);
	showDialog("您今天選擇數已經超出限制了，請明天再來哦！！");
}

function loaded_user(user) {
	log("loaded_user: ", user);
}

function sexConvert(gender) {
	var sex = 2;
	switch(gender) {
	case 'male': case '男性': case '男': case 'laki-laki': case 'lelaki': case 'erkek': case 'homme':
		sex = 0;
		break;
	case 'female': case '女性': case '女': case 'perempuan': case 'nữ': case 'หญิง':
		sex = 1;
		break;
	default:
		sex = 2;
	}
	
	return sex;
}

function loaded_friends(friends) {
	log("loaded_friends: ", friends);

	jQuery.post('action.php?cmd=save_friends&mid='+g_user.mid, {'friends' : friends});
}

function getFbImage(sitemid) {
	return "http://graph.facebook.com/"	+ sitemid + "/picture?type=large";
}

function getFbProfile(sitemid) {
	return "http://www.facebook.com/profile.php?id=" + sitemid;
}

function showImage(jid, sitemid, width, height) {
	jid.fadeOut(500, function() {
		jid.attr('src', getFbImage(sitemid));
		jid.attr('pid', sitemid);
		jid.fadeIn(500);
		
		jid.load(function(){
		  resizeImg(this, width, height);
		});
	});
}

function init_battle() {
	jQuery("#pic-l").html("<img id='pic_l' src='" + defaultImg + "' />");
	jQuery("#pic-r").html("<img id='pic_r' src='" + defaultImg + "' />");
	
	jQuery.post('action.php?cmd=init_battle&mid='+ g_user.mid +'&sex='+p_user.sex, null, function(data) {
		log("init_battle", data.flag);
		if(data.flag == -10) {
			overLimit();
			return false;
		}else if(data.flag == 1) {
			showImage(jQuery("#pic_l"), data.new_battle[0].sitemid, 286, 286);
			showImage(jQuery("#pic_r"), data.new_battle[1].sitemid, 286, 286);
		}
	}, "json");
}

function resizeImg(img, maxWidth, maxHeight){
	var w = img.width, h = img.height;
	log("resizeImg", [w, h]);
	if(w/h > maxWidth/maxHeight) {
		img.height=maxHeight; 
	}else {
		img.width=maxWidth;
	}
}; 

function do_battle(w) {
	var data = {
		pic_l: jQuery('#pic_l').attr('pid'),
		pic_r: jQuery('#pic_r').attr('pid'),
		win: w,
		mid: g_user.mid
	};
	
	log("do_battle", data);
	
	jQuery.ajax({
		type: 'POST',
		url: "action.php?cmd=do_battle",
		data: data,
		success: function(data) {
		
			log("do_battle", data);
			
			if(data.flag == -10) {
				overLimit();
				return false;
			}
			if(data.flag != 1) return false;
			
			jQuery('#surplus').html(data.surplus);
			
			do_results(data.results, w);
			init_battle(data.new_battle);
		},
		error: function() {
			log("do_battle", 'error.....');
		},
		dataType: "json"
	});	
}

function do_results(results, w) {
	log("do_results", results);
	
	var pic_l_pid = jQuery('#pic_l').attr('pid');
	var pic_r_pid = jQuery('#pic_r').attr('pid');
	
	jQuery('#pic_l_thumba').attr('href', getFbProfile(pic_l_pid));
	jQuery('#pic_r_thumba').attr('href', getFbProfile(pic_r_pid));
	
	showImage(jQuery("#pic_1_thumb"), jQuery('#pic_l').attr('pid'), 80, 80);
	showImage(jQuery("#pic_r_thumb"), jQuery('#pic_r').attr('pid'), 80, 80);
	
	var jid = null, id = null;
	for(var i in results) {
		jid = null, id = null;
		if(results[i].sitemid == pic_l_pid) {
			jid = jQuery('#pic_1_results');
			id = 'pic_1_results';
		}else if(results[i].sitemid == pic_r_pid) {
			jid = jQuery('#pic_r_results');
			id = 'pic_r_results';
		}
		
		if(id != null) {
			jid.html("<fb:name uid='" + results[i].sitemid + "' linked='false' /><br/><strong>" + results[i].wins + "</strong> 贏,  <strong>" + results[i].losses + "</strong> 輸");
			FB.XFBML.parse(document.getElementById(id));
		}
	}
	
	if (!w) {
		jQuery('#pic-results-l').addClass('winner');
		jQuery('#pic-results-r').removeClass('winner');
	} else {
		jQuery('#pic-results-r').addClass('winner');
		jQuery('#pic-results-l').removeClass('winner');
	}
	jQuery('#results').show();
}

function show_user(puid) {
	log("show_user", puid);
	
	if(typeof(puid) == 'undefined') return;
	
	showContain(
		'show_user_view',
		{	},
		function() {}, 
		'&puid='+puid, 
		false, 
		{}
	);
}

function showDialog(msg) {
	jQuery.facebox(msg);
}

function showContain(name, css, fn, p, isXFBML, postdata) {
	if ('undefined' == typeof (p)) {
		p = '';
	}
	if ('undefined' == typeof (css)) {
		css = '';
	}
	if ('undefined' == typeof (fn) || fn == '') {
		fn = function() {
		};
	}
	if ('undefined' == typeof (isXFBML)) {
		isXFBML = false;
	}
	
	if ('object' != typeof (postdata)) {
		postdata = {};
	}
	
	var url = 'action.php?cmd=' + name + p;
	
	
	jQuery.post(url, postdata, function(data) {
		log("showContain||name", data);
	}, "json");	

/*	
	jQuery.post(url, postdata, function(data){
		log("showContain", data);
		$(document).bind("reveal.facebox", function (event, message1, message2) {
			$(document).unbind("reveal.facebox");

			FBParse(document.getElementById('facebox'), function(){
				$('#facebox').css('left', $(window).width() / 2 - ($('#facebox .popup').width() / 2));	
				if($('#wrapper').height() < $('#facebox').height()) $('#wrapper').css('height', $('#facebox').height() + 100);
			});
			
			$('#facebox').css('left', $(window).width() / 2 - ($('#facebox .popup').width() / 2));
			if($('#wrapper').height() < $('#facebox').height()) $('#wrapper').css('height', $('#facebox').height() + 100);
			fn();
		});
		$.facebox(data);
	});
*/	
	
}

function log(funcName, param) {
	if(typeof(console) != 'undefined' && param != null ) {
		str = funcName + "========================\n";
		switch(typeof(param)) {
			case 'number':
			case 'boolean':
			case 'string':
				str = str + param;
				break;
			case 'object':
				for(var k in param) {
					str = str + " k = " + k + " v = " + param[k] + "\n";  
				}
				break;
		}
		
		console.log(str);
	}
}

//重写某某方法
$.extend($.facebox, {
	reveal: function(data, klass) {
		$(document).trigger('beforeReveal.facebox');
		if (klass) $('#facebox .content').addClass(klass);
		$('#facebox .content')[0].innerHTML = data;
		$('#facebox .loading').remove();
		$('#facebox .body').children().fadeIn('normal');
		$('#facebox').css('left', $(window).width() / 2 - ($('#facebox .popup').width() / 2));
		$(document).trigger('reveal.facebox').trigger('afterReveal.facebox');
	}
	,
	settings: {
		opacity      : null,
		overlay      : false,
		loadingImage : cdn_url + 'facebox/loading.gif',
		closeImage   : cdn_url + 'facebox/closelabel.png',
		imageTypes   : [ 'png', 'jpg', 'jpeg', 'gif' ],
		faceboxHtml  : '\
    <div id="facebox" style="display:none;"> \
      <div class="popup"> \
        <div class="content"> \
        </div> \
        <a href="#" class="close"><img src="'+ cdn_url +'facebox/closelabel.png" title="close" class="close_image" /></a> \
      </div> \
    </div>'
    }
});